apiready = function() {
seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../pop"], function(V, $api, $, $app, service, $pop) {
  $app.ready();
    var vm = {
        el: '.container',
        data: {
            pay_key: '',
            pay_channel: '',
            pay_id: 0,
            payIndex: -1,
            money: 0,
            is_group: false,
            name: ''
        },
        methods: {
            //--选择支付方式
            goPayment: function(et) {
                var unionid = $api.getStorage("shop_unionid"),
                    self = this;
                    this.pay_id = +et.currentTarget.dataset.id;
                    var url = service.buyOrderPayment;
                    this.payIndex = +et.currentTarget.dataset.index;
                    $app.ajax(url, {
                        data: {
                            values: {
                                unionid: unionid,
                                pay_key: self.pay_key,
                                pay_id: self.pay_id,
                                from: 'shop_app'
                            }
                        }
                    }, function(res) {
                        //console.log("go_pay: "+JSON.stringify(res));
                        if (res.code == 0) {

                            if (self.pay_id == 2 || self.pay_id == 6) {
                              //同步返回结果：
                              var installed = api.appInstalled({
                                  sync: true,
                                  appBundle: 'alipayqr://'
                              });
                              if (installed) {
                                //--支付宝支付
                                var aliPayPlus = api.require('aliPayPlus');
                                aliPayPlus.payOrder({
                                    orderInfo: res.data
                                }, function(ret, err) {
                                    // 9000成功
                                    if (ret.code == 9000) {
                                        //支付成功
                                        if (self.is_group) {
                                            //--拼团
                                            $app.openWin("buyGroupPayWin", {
                                                orderId: self.pay_key,
                                                name: self.name
                                            });
                                        } else {
                                            //--正常
                                            $app.openWin("buyOrderPayResult", {
                                                money: self.money,
                                                name: self.name
                                            });
                                        }
                                        api.sendEvent({
                                            name: 'payBackGoods_'
                                        });
                                    } else {
                                        //--取消支付
                                        api.toast({
                                            msg: '支付失败！',
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                    }

                                });
                              } else {
                                  //应用未安装
                                  $app.toast('请前往Apple Store安装支付宝APP');
                              }
                            } else if (self.pay_id == 3 || self.pay_id == 7) {
                              //同步返回结果：

                              if ($app.appInstalled()) {
                                //--微信支付
                                var wxPay = api.require('wxPay');
                                wxPay.payOrder({
                                    orderId: res.data.prepayid,
                                    mchId: res.data.partnerid,
                                    nonceStr: res.data.noncestr,
                                    timeStamp: res.data.timestamp,
                                    package: res.data.package,
                                    sign: res.data.sign,
                                    apiKey: res.data.appid
                                }, function(ret, err) {
                                    //console.log(self.money);
                                    if (ret.status) {
                                        //支付成功
                                        if (self.is_group) {
                                            //--拼团
                                            $app.openWin("buyGroupPayWin", {
                                                orderId: self.pay_key,
                                                name: self.name
                                            });
                                        } else {
                                            //--正常
                                            $app.openWin("buyOrderPayResult", {
                                                money: self.money,
                                                name: self.name
                                            });
                                        }
                                        api.sendEvent({
                                            name: 'payBackGoods_'
                                        });
                                    } else {
                                        api.toast({
                                            msg: '支付失败！',
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                    }
                                });
                              }else{
                                $app.toast('您当前没有安装微信，请前往Apple Store下载安装微信APP')
                              }
                            }
                        }
                    });
            },
        },
        mounted: function() {
            var self = this;

            function btnKeyBack() {
                api.sendEvent({
                    name: 'payBackGoods_'
                });
                api.sendEvent({
                    name: 'payBackCart_'
                });
                $api.setTimeout(function() {
                    api.closeToWin({
                        name: self.name
                    });
                }, 200);
            }
            api.addEventListener({
                name: 'keyback'
            }, function() {
                btnKeyBack();
            });

            //--点击上面按钮返回
            $api.addEvt($api.dom('#btn-back-new'), 'click', function() {
                btnKeyBack();
            });

            //--右滑
            api.addEventListener({
                name: 'swiperight'
            }, function(ret, err) {
                btnKeyBack();
            });
        },
        created: function() {
            this.pay_key = api.pageParam.pay_key;
            this.pay_channel = api.pageParam.pay_channel;
            this.money = api.pageParam.money;
            this.is_group = api.pageParam.is_group;
            this.name = api.pageParam.name || api.pageParam.prve;
        }
    };
    //var shopVue = new Vue(vm);
    var newVue = new Vue(vm);
    var even = function() {

    }
    even();
})
}
